<?php
/*
Plugin Name: TagCloud
Plugin URI: http://www.yhustc.com/
Description: Tag云图
Author: yhustc
Version: 1.0
Author URI: http://www.yhustc.com/
*/

class TagCloudWidget extends Widget
{
	protected $smallest = 8; //tag最小的字号,单位pt
	protected $largest = 22; //tag最大的字号,单位pt
	protected $limit = 15; //显示的标签数

	function render($data=array())
	{
		$tagList = S("tagList");
		if(!$tagList)
		{
			$tagDao = new Model();
			$tagList = $tagDao->query("SELECT COUNT(postid) AS num,tag_name FROM `".C("DB_PREFIX")."tags` GROUP BY tag_name ORDER BY num DESC LIMIT ".$this->limit);
			S("tagList",$tagList);
		}

		$counts = array();
		foreach($tagList as $tag)
			$counts[$tag["tag_name"]] = $tag["num"];

		$minCout = min($counts);
		$spread = max($counts) - $minCout;
		if ( $spread <= 0 )
			$spread = 1;
		$font_spread = $this->largest - $this->smallest;
		$font_step = $font_spread / $spread;
		
		if(count($tagList) > 0)
		{
			$options = S("options");
			$this->assign("options",$options);
			$this->assign("smallest",$this->smallest);
			$this->assign("tagList",$tagList);
			$this->assign("minCout",$minCout);
			$this->assign("font_step",$font_step);
			return $this->renderFile();
		}
	}
}
?>